package other.mvp.activity

import com.android.ide.common.symbols.parseManifest
import com.android.tools.idea.wizard.template.ModuleTemplateData
import com.android.tools.idea.wizard.template.RecipeExecutor
import other.mvp.activity.layout.mvpActivityXml
import other.mvp.activity.manifest.mvpActivityManifestXml
import other.mvp.activity.src.mvpActivityKt
import other.mvp.activity.src.mvpContractKt
import other.mvp.activity.src.mvpModelKt
import other.mvp.activity.src.mvpPresenterKt

fun RecipeExecutor.mvpActivityRecipe(
        moduleData: ModuleTemplateData,
        activityClass: String,
        layoutName: String,
        packageName: String
) {
    val (projectData, srcOut, resOut, manifestDir) = moduleData
    val ktOrJavaExt = projectData.language.extension
    //判断在manifest中是否存在launcherActivity
    val isNewModule = parseManifest(manifestDir.resolve("AndroidManifest.xml"))?.launcherActivity == null
    //manifest文件中追加信息
    mergeXml(mvpActivityManifestXml(projectData.applicationPackage, activityClass, layoutName, packageName, isNewModule), manifestDir.resolve("AndroidManifest.xml"))
    //保存contract
    save(mvpContractKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass}/mvp/I${activityClass}Contract.${ktOrJavaExt}"))
    //保存activity
    save(mvpActivityKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass}/${activityClass}Activity.${ktOrJavaExt}"))
    //保存xml
    save(mvpActivityXml(projectData.applicationPackage, activityClass, layoutName, packageName), resOut.resolve("layout/${layoutName}.xml"))
    //保存presenter
    save(mvpPresenterKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass}/mvp/${activityClass}Presenter.${ktOrJavaExt}"))
    //保存model
    save(mvpModelKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass}/mvp/${activityClass}Model.${ktOrJavaExt}"))
}